<?php
/**
 * Created by PhpStorm.
 * User: Ysll <754060604@qq.com>
 * Date: 2017/9/29
 * Time: 17:19
 */

namespace Common\Model;


class Role
{
    public $id;
    public $name;
    public $display_name;
    public $description;
    public $flag;
    public $is_default;
    public $updated_at;
    public $created_at;
    public $isQuery = false;

    public $permissions = array();

    public function __construct($id)
    {
        $this->id = $id;
        $this->init();
    }

    /**
     * 初始化
     */
    private function init()
    {
        $model = M('roles');
        $result = $model->where('id = ' . $this->id)->find();
        if (!empty($result)) {
            $this->isQuery = true;
            $this->id = $result['id'];
            $this->name = $result['name'];
            $this->display_name = $result['display_name'];
            $this->flag = $result['flag'];
            $this->is_default = $result['is_default'];
            $this->description = $result['description'];
            $this->updated_at = $result['updated_at'];
            $this->created_at = $result['created_at'];
            $this->initPermission();
        } else {
            $this->isQuery = false;
        }
    }

    /**
     * 初始化许可
     */
    private function initPermission()
    {
        $rolePermission = new PermissionRoles($this->id);
        $this->permissions = $rolePermission->item;
    }

    /**
     * 判断是否是空模型
     *
     * @return bool
     */
    public function emptyModel()
    {
        return $this->isQuery;
    }

    /**
     * 判断是否是？？
     *
     * @param string $name 角色名称
     * @return bool
     */
    public function is($name)
    {
        return $this->name === $name;
    }

    /**
     * 判断是否拥有某个许可
     *
     * @param string $name 许可名称
     * @return bool
     */
    public function hasPermission($name)
    {
        foreach ($this->permissions as $permission) {
            if ($permission->name === $name) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否拥有某些许可
     *
     * @param array $array 许可
     * @return boolean
     */
    public function hasPermissions(array $array)
    {
        $permissions = array();
        foreach ($this->permissions as $permission) {
            $permissions[] = $permission->name;
        }
        $result = array_intersect($permissions, $array);
        return empty(array_diff($array, $result));
    }

    /**
     * 获取默认的角色
     *
     * @return null|Role
     */
    public static function defaultRole()
    {
        $model = M('roles');
        $data = $model->where('is_default = 1')->find();
        if (empty($data)) {
            return null;
        }
        return new static($data['id']);
    }

    /**
     * 创建角色 关联permission
     *
     * @param array $data
     * @param array|null $permissions
     * @return bool
     */
    public static function add(array $data, array $permissions = null)
    {
        $roles = M('roles');
        $id = $roles->add($data);
        if ($id !== false) {
            PermissionRoles::creates($id, $permissions);
        }
        return true;
    }

    public static function update($id, array $data, array $permissions = null) {
        M('roles')->where('id = ' . $id)->save($data);
        PermissionRoles::deleteByRoleId($id);
        if ($permissions != null) {
            PermissionRoles::creates($id, $permissions);
        }
    }
}